﻿#include "uimain.h"
#include "qpushbutton.h"
#include "ui_uimain.h"
#include "src/middle/config/common.h"
#include <QMessageBox>
#include <src/GUI/uicom/uimessagebox.h>
#include <src/GUI/uitransfer/uitransfer.h>

UiMain::UiMain(QWidget *parent): UiQosDialog(parent), ui(new Ui::UiMain)
{
    ui->setupUi(body());
    ui->splitter->setStretchFactor(0, 1); // 左右比例初始为 1:4
    ui->splitter->setStretchFactor(1, 4);

    // 添加一个传输进度按钮
    QPushButton *transBtn =
            addButton(GLOBAL::PATH::TRANS, GLOBAL::PATH::TRANS_HOVER);
    connect(transBtn, &QPushButton::clicked, this, &UiMain::showTransfer);

    // 添加一个退出登录按钮
    QPushButton *quitBtn =
            addButton(GLOBAL::PATH::QUIT, GLOBAL::PATH::QUIT_HOVER);
    connect(quitBtn, &QPushButton::clicked, MG->mSignal, &ManSignals::unLogin);

    // 添加一条线
    addTitleLine();

    // 添加一个最小化按钮
    addMinButton(GLOBAL::PATH::MIN_PATH, GLOBAL::PATH::MIN_HOVER_PATH);

    // 添加一个最大化按钮
    addMaxButton(
            GLOBAL::PATH::MAX_PATH, GLOBAL::PATH::MAX_HOVER_PATH,
            GLOBAL::PATH::NORMAL_PATH, GLOBAL::PATH::NORMAL_HOVER_PATH);

    setTitle(STR("OS Viewer"));
    resize(1080, 640);

    // 禁用esc或回车关闭窗口
    setKeyDisabled();

    connect(MG->mSignal, &ManSignals::loginSuccess, this, &UiMain::show);
    connect(MG->mSignal, &ManSignals::unLogin, this, &UiMain::onUnLogin);
    connect(MG->mSignal, &ManSignals::bucketsSuccess, this,
            &UiMain::onBucketsSuccess);
    connect(MG->mSignal, &ManSignals::objectsSuccess, this,
            &UiMain::onObjectsSuccess);
    connect(MG->mSignal, &ManSignals::error, this, &UiMain::onError);

    m_transfer = new UiTransfer(this);
}

UiMain::~UiMain()
{
    delete ui;
}

void UiMain::onUnLogin()
{
    hide();
}

void UiMain::onBucketsSuccess(const QList<MyBucket> &buckets)
{
    // 切换到 buckets 列表界面
    // UiBucketsTableWidget
    ui->stackedWidget->setCurrentIndex(0);
}

void UiMain::onObjectsSuccess(const QList<MyObject> &objects)
{
    // 切换到 objects 列表界面
    // UiObjectsTableWidget
    ui->stackedWidget->setCurrentIndex(1);
}

void UiMain::showTransfer()
{
    // 弹出传输进度界面
    m_transfer->show();
}

void UiMain::onError(int api, const QString &msg, const QJsonValue &req)
{
    if (isVisible() && api > API::BUCKETS::BASE) {
        UiMessageBox().showMessage(STR("错误"), msg);
    }
}
